<script setup>
import { ref,onMounted } from 'vue'
import { queryOutboundOrderPackage,updateOutboundOrderPackage } from '../../api/outboundorderpackage'
import { getAllUser } from '../../api/user'
import { ElMessage } from 'element-plus'

//定义userlist集合
const userList = ref([])
const getUserList = async () => { 
  const res = await getAllUser()
  userList.value = res.data
}
//分页条件查询
const query = async () =>{
  const result = await queryOutboundOrderPackage(page.value,pageSize.value,outBoundOrderPackageFrom.value.status,outBoundOrderPackageFrom.value.packageCode,outBoundOrderPackageFrom.value.customerName)
  outBoundOrderPackageList.value = result.data.rows
  total.value = result.data.total
}
//清空表单数据
const clear = () => {
  outBoundOrderPackageFrom.value = { status: '', packageCode: '', customerName: ''}
  query()
}

//定义表格数据
const outBoundOrderPackageList = ref([])

//定义表单数据
const outBoundOrderPackageFrom = ref({ status:'', packageCode:'', customerName:''})

//定义弹出框数据
const title = ref(null)
const dialogShow = ref(false)
const dialogData = ref({count:'',packageEmpId:'',packageTime:''})

//定义分页的变量
const page = ref(1)
const pageSize = ref(10)
const total = ref(0)
//定义分页的点击事件
const handleSizeChange = (val) => {
  page.value = 1
  query()
}
const handleCurrentChange = (val) => {
  query()
}

//挂载
onMounted(() => { 
  query()
  getUserList()
})

//定义确定打包的点击事件
const confirm = (row) => { 
  title.value = '打包'
  dialogShow.value = true
  dialogData.value.count = row.packageCount
  info.value.id = row.id
}
//定义接收的变量
const info = ref({id:'',count:'',packageEmpId:'',packageTime:''})
//确认打包信息的点击事件
const confirmPackageInfo = async ()=>{ 
  info.value.count = dialogData.value.count
  info.value.packageEmpId = dialogData.value.packageEmpId
  info.value.packageTime = dialogData.value.packageTime
  const result = await updateOutboundOrderPackage(info.value)
  if(result.code){
    ElMessage.success('操作成功')
    query()
    dialogShow.value = false
    //清空表单数据
    dialogData.value = {count:'',packageEmpId:'',packageTime:''}
    //重置变量里面的数据
    info.value = {id:'',count:'',packageEmpId:'',packageTime:''}
  }else{
    ElMessage.error('操作失败！请重新操作')
  }
}
</script>
<template>
<div><h3>待打包</h3></div>

<div>
  <el-form :inline="true" :model="outBoundOrderPackageFrom" class="demo-form-inline">
    <el-form-item>
      <el-select v-model="outBoundOrderPackageFrom.status" placeholder="请选择状态" style="width: 200px;">
        <el-option label="未打包" value="1"></el-option>
        <el-option label="已打包" value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-input v-model="outBoundOrderPackageFrom.packageCode" placeholder="打包单号" style="width: 200px;"/>
    </el-form-item>
    <el-form-item>
      <el-input v-model="outBoundOrderPackageFrom.customerName" placeholder="客户名称" style="width: 200px;"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="query" plain>查询</el-button>
      <el-button type="default" @click="clear" plain>清空</el-button>
    </el-form-item>
  </el-form>
</div>

<!-- table表格 -->
<div>
  <el-table :data="outBoundOrderPackageList" border style="width: 100%" >
    <el-table-column fixed type="index" label="序号" align="center"  width="80"/>
    <el-table-column prop="packageCode" label="打包单号" align="center"  width="200"></el-table-column>
    <el-table-column prop="status" label="拣货状态" align="center" width="120">
      <template #default="scope">
        <el-tag v-if="scope.row.status == 1" type="info">未打包</el-tag>
        <el-tag v-else="scope.row.status == 2" type="success">已打包</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="customerName" label="客户名称" align="center" width="200"/>
    <el-table-column prop="skuCode" label="商品编码" align="center" width="200"/>
    <el-table-column prop="skuTitle" label="商品名称" align="center" width="200"/>
    <el-table-column prop="packageCount" label="待打包数量" align="center" width="150"/>
    <el-table-column prop="packageWeight" label="待打包总重量" align="center" width="150"/>
    <el-table-column prop="packageVolume" label="待打包总体积" align="center" width="150"/>
    <el-table-column prop="createBy" label="创建人" align="center" width="150"/>
    <el-table-column prop="packageTime" label="打包时间" align="center" width="200">
      <template #default="scope">
        <span>{{ scope.row.packageTime || '未打包' }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="pickEmpId" label="打包员" align="center" width="150">
      <template #default="scope">
        <span>{{ userList.find(user => user.id == scope.row.pickEmpId)?.name || '未打包'}}</span>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建时间" align="center" width="200"/>
    <el-table-column fixed="right" label="操作" align="center" width="80">
      <template #default="scope">
        <el-button type="primary" size="small" @click="confirm(scope.row)" plain><el-icon><Select /></el-icon></el-button>
      </template>
    </el-table-column>
  </el-table>
</div>

<!-- 分页 -->
<div>
  <el-pagination
  v-model:current-page="page"
  v-model:page-size="pageSize"
  :page-sizes="[5, 10, 15, 20]"
  layout="total, sizes, prev, pager, next, jumper"
  :total="total"
  @size-change="handleSizeChange"
  @current-change="handleCurrentChange"/>
</div>

<!-- 弹出框 -->
<div>
  <el-dialog v-model="dialogShow" :title="title" width="500" align-center>
    <el-form :model="dialogData">
      <el-form-item>
        <el-input v-model="dialogData.count" placeholder="输入打包的数量" style="width: 350px;"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dialogData.packageEmpId" placeholder="请选择打包员" style="width: 350px;">
          <el-option v-for="user in userList" :label="user.name" :value="user.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="dialogData.packageTime" type="date" placeholder="打包时间" value-format="YYYY-MM-DD" style="width: 350px;" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogShow = false" type="default">取消</el-button>
      <el-button type="primary" @click="confirmPackageInfo">确定</el-button>
    </template>
  </el-dialog>
</div>
</template>
<style scoped>
</style>